abstract class Book(val name: String)
class Fiction(name: String):Book(name)
class NonFiciton(name:String):Book(name)

val books:List<Book> = listOf(Fiction("MobyDIck"),NonFiciton("learnToCode"),Fiction("LOTR"))

fun <T> findFirst(books:List<Book>,ofClass:Class<T>):T{
    val selected = books.filter { book -> ofClass.isInstance(book)  }
    if(selected.isEmpty()){
        throw RuntimeException("Not Found")
    }
    return ofClass.cast(selected[0])
}
inline fun <reified T> findFirst(books:List<Book>):T{
    val selected = books.filter { book -> book is T }
    if(selected.isEmpty()){
        throw RuntimeException("Not Found")
    }
    return selected[0] as T
}
println(findFirst(books,NonFiciton::class.java).name)
println(findFirst<NonFiciton>(books).name)
